#!/bin/bash

declare -A roles
for constant in $APP_ROLE
do
    roles[$constant]=1
done
if [[ ! -f /root/.app_laravel_inited ]]; then
  exit
fi

cd /var/www


if [[ ${roles["queue_daemon"]} ]]; then
  ./artisan schedule:run
fi


git fetch
NEED_RELEASE=$(git diff $APP_GIT_BRANCH origin/$APP_GIT_BRANCH RELEASE_CODE|wc -l)
NEED_CC=$(git diff $APP_GIT_BRANCH origin/$APP_GIT_BRANCH RELEASE_CODE|grep +CACHE_VERSION:|wc -l)
NEED_CONSUMER_RESTART=$(git diff $APP_GIT_BRANCH origin/$APP_GIT_BRANCH RELEASE_CODE|grep +CONSUMER_VERSION:|wc -l)

if [[ $NEED_RELEASE -lt 1 ]]; then
  echo 'No need release.';
  exit
fi

git pull
cp .env.prod .env

git submodule init
git submodule update


if [[ $NEED_CC -gt 0 ]]; then
  ./artisan config:cache 
  ./artisan route:cache  
  ./artisan optimize
fi

if [[ $NEED_CONSUMER_RESTART -gt 0 ]]; then
  if [[ ${roles["queue_daemon"]} ]]; then
    ./artisan queue:restart
    sudo -u www-data ./artisan queue:work --tries=1 redis &
  fi
fi
